//展示页面
kitkatApp.controller('PageCtrl', function ($scope,pageHelper) {
	 var projectId = $("div[projectId]").attr("projectId");
	 pageHelper.init(projectId).success(function(response) {
		 var project = response.result;
	  	 var pages = eval("(" + project.content + ")");
	  	 pageHelper.save(pages);
	  	 var length = pages.length;
	  	 for(var i=0;i<length;i++) {
              if(pages[i].index == 1) {
                  $scope.page = pages[i];
              }
         }
		 $scope.$watch('page', function(newVal, oldVal) { //监听页面内容是否发生变化
			   $("#paper div:first").attr("class",$scope.page.entrance);
		 });

		 $scope.prevPage = function() {
		  	  $("#paper div:first").one('webkitAnimationEnd',function(){
		          var page = pageHelper.prevPage()
		  		  if(page){
		        	  $scope.page = page; 
		          }  
		          $scope.$apply();
		    	});
		  	  var pageNumber = pageHelper.getCurrentPageNumber();
		  	  if(pageNumber > 0) {
		  		 $("#paper div:first").removeClass().addClass($scope.page.exit); 	
		  	  }
		 };

		 $scope.nextPage = function() {
		  	$("#paper div:first").one('webkitAnimationEnd',function(){
		  		 var page = pageHelper.nextPage()
		  		  if(page){
		        	  $scope.page = page; 
		          }  
		          $scope.$apply();
		    	});
		  	  var pageNumber = pageHelper.getCurrentPageNumber();
		  	  var length = pageHelper.getCurrentpageLength();
		  	  if(pageNumber +1 < length) {
		  		$("#paper div:first").removeClass().addClass($scope.page.exit);
		  	  }
		       
		 };
		 
		 //取消loading效果
 	     $scope.loading = false;
	 });
});

//注册
kitkatApp.controller('register', function ($scope,$window,ngAlert,register) {
	$scope.register = function() {
		register.save($scope.user).success(function(response) {
			console.log(response);
			ngAlert.Alert("注册成功",function() {
				$window.location.href = "pageLogin.do";
			});
		});
	};
});


//登录
kitkatApp.controller('login', function ($scope,$window,ngAlert,login) {
	$scope.user = {"email":"521017853@qq.com",
			"password":"1"};
	$scope.login = function() {
		login.save($scope.user).success(function(response) {
			ngAlert.Alert("登录成功",function() {
				$window.location.href = "PageManage.do";
			});
		});
	};
});

//管理工程
kitkatApp.controller('manage', function ($scope,$window,manage,ngAlert,Upload) {
	$scope.chooseTemplate = function(templateId) {
		//manage.addProject(templateId);
		manage.addProject("").success(function(response) {
			ngAlert.Alert("添加项目成功",function() {
				$window.location.href = "PageManage.do";
			});
		});
	};
	manage.getProjects().success(function(response) {
		$scope.projects = response.result;
	});
	//保存项目名称和描述
	$scope.saveProject = function() {
		var projectId = $scope.editingProId;
		if(projectId) {
			var project = {};
			project.id = projectId;
			project.name = $("#" + projectId + " div[name]").html();
			project.description = $("#" + projectId + " div[des]").html();
			manage.updateProject(project).success(function(response) {
				ngAlert.autoHide("保存成功");
				$scope.editingProId = "";
			});
		}
	};
	//修改项目名称和描述
	$scope.changeProContent = function(projectId,$event) {
		if($scope.editingProId != projectId) {
			$scope.saveProject();
		}
		$scope.editingProId = projectId;//记录正在编辑的项目id
		$event.stopPropagation();
	};
	//编辑项目内容
	$scope.editProject = function(projectId) {
		$window.open("edit.do?projectId=" + projectId);
	}
	//删除项目
	$scope.deleteProject = function(projectId) {
		ngAlert.Confirm("您确定删除",function() {
			manage.deleteProject(projectId).success(function() {
				ngAlert.Alert("删除成功",function() {
					$window.location.href = "PageManage.do";
				});
			});
		});
	};
});

//编辑页面
kitkatApp.controller('PageListCtrl', function ($scope,$document,$window,Upload,UUID,pageHelper,manage,ngAlert) {
    var projectId = $("div[projectId]").attr("projectId");
	$scope.selectedNum = 0;//初始化页面为第一页
    pageHelper.init(projectId).success(function(response) {
    	   var project = response.result;
    	   var projectId = project.id;
    	   $scope.pages = eval("(" + decodeURI(project.content) + ")");
    	   console.log($scope.pages);
    	   var length = $scope.pages.length;
    	   for(var i=0;i<length;i++) {
    		   if($scope.pages[i].index == 1) {
    			   $scope.page = $scope.pages[i];
    		   }
    	   }
    	   $scope.selectedPageId = $scope.page.pageId;

    	   $scope.$watch('page', function(newVal, oldVal) { //监听页面内容是否发生变化
    		   $("#pageWrap div:first").attr("class",$scope.page.entrance);
    	   });
    	   //保存项目
    	   $scope.saveProject = function() {
    		   var project = {};
    		   project.id = projectId;
    		   project.content = encodeURI(JSON.stringify($scope.pages));
    		   manage.updateProject(project).success(function() {
    			  ngAlert.autoHide("项目保存成功");
    		   });
    	   };
    	  //jquery剪裁器修改背景
    	    var setChange = function (c){
    	          var x = c.x;
    	          var y = c.y;
    	          /*var x2 = c.x2;
    	          var y2 = c.y2;*/
    	          var width = c.w;
    	          /* var height = c.h;*/
    	          var times = 150/width;
    	          var timesx = x/150;
    	          var timesy = y/235;
    	          var style = $scope.page.backImg.style;
    	          style.width = 300*times;
    	          style.height = 470*times;
    	          style.left = -style.width*timesx;
    	          style.top = -style.height*timesy;
    	          $scope.$apply();
    	    };
    	    
    	    //选中第几页
    	    $scope.choosePage = function(pageId) {
    	        if($scope.selectedPageId == pageId) {//如果点击当前所在页面就返回
    	        	return;
    	        }
    	        
    	        $scope.selectedPageId = pageId;
    	    	$("#pageWrap div:first").one('webkitAnimationEnd',function(){
    	    	    //$scope.page = $scope.pages[$scope.selectedNum]//pageHelper.getPage(index);
    	            $scope.page = getPage(pageId);
    	            
    	            $scope.jcrop.destroy();//切换页面先暂停jquery剪裁器  
    	    	    
    	            $scope.$apply();
    	            
    	    	    $("img[jcrop]").Jcrop({//重启jquery剪裁器
    	              onChange:setChange,
    	              onSelect:setChange,
    	              aspectRatio:0.638
    	            },function(){
    	               $scope.jcrop = this;
    	            });


    	            $("div[edit]").one("webkitAnimationEnd",function(){//去掉动画的样式才可以修改透明度和旋转等样式
    	                $(this).removeClass();
    	            });
    	    	});
    	    	$scope.selectEle.ele = "";//取消选中样式
    	    	$scope.selectEle.id = "";
    	    	$scope.selectEle.tagName = "bg";//默认是修改背景
    	        $scope.showTextColorPicker = false;//隐藏颜色选择器
    	        $scope.showBorderColorPicker = false;//隐藏边框颜色选择器
    	        $scope.showBackEleColorPicker = false;//隐藏元素背景颜色选择器
    	        $scope.showBackColorPicker = false;//隐藏背景颜色选择器
    	        $("#pageWrap div:first").removeClass().addClass($scope.page.exit);
    		};
    	    
    		//中间栏鼠标点击事件委托
    		$scope.centerEventHandle = function($event) {
    			$scope.showPictureStroe = false;//关闭图片显示库
    	        var targetEle = $event.target;
    	        if(targetEle.tagName == "IMG" && targetEle.className == "backImg" || targetEle.className.indexOf("elements") == -1){//选中的不是可编辑的元素
    	        	$scope.selectEle.tagName = "bg";
    	            $scope.selectEle.ele = "";//取消选中样式
    	            $scope.selectEle.id="";
    	            $scope.showTextColorPicker = false;//隐藏颜色选择器
    	        }
    		};
    	    
    		var activeAction = function(event,ui) {
    	    	var style = event.target.style;
    	        var selectele = $scope.selectEle.ele;
    	        selectele.style.left = style.left;
    	        selectele.style.top = style.top;
    	        selectele.style.width = style.width;
    	        selectele.style.height = style.height;
    	        $scope.$parent.$apply();
    	    };
    		var resizeConfig = {
    	            resize:activeAction,
    	            autoHide:true
    	        };
    		
    	    //文字鼠标按下事件
    		$scope.mousedown = function(id,$event) {
    	         if($scope.selectEle.id != id || !$scope.selectEle.id) {//只有选中不同的元素才触发以下事件
    	        	var targetEle = $event.target;
    	            var wrapEle = targetEle.parentNode;
    	            var imgLength = wrapEle.getElementsByTagName("img").length;
    	            if(imgLength > 0) {
    	            	$scope.selectEle.tagName = "img";
    	            }else{
    	            	$scope.selectEle.tagName = "text";
    	            }
    	        	$scope.selectEle.id = id;//选中样式(id标识记录在当前编辑页的元素)
    	        	var length = $scope.page.eles.length;
    	        	for(var i=0;i<length;i++) {
    	        		var ele = $scope.page.eles[i];
    	        		if(ele.id == id) {
    	        			$scope.selectEle.ele = ele;//被选中的元素
    	        		}
    	        	}
    	         }
    	         $event.stopPropagation();
    		};
    		
    		$document.bind("keydown", function(event) {
	    			$scope.$apply(function (){
	    				var ele = $scope.selectEle.ele;
		    			if(ele){
		    				if(event.keyCode == 37){
		    					ele.style.left--;
					        }
		    			    if(event.keyCode == 38){
		    		            ele.style.top--;
		    		        }
		    			    if(event.keyCode == 39){
		    			    	ele.style.left++;
		    		        }
		    		        if(event.keyCode == 40){
		    		        	ele.style.top++;
		    		        }
		    			}
		    			
	    		    });
    		});
    		
    	    //修改文字粗细
    	    $scope.toggleFontWeight = function() {
    	    	var selectele = $scope.selectEle.ele;
    	    	var weight = selectele.style.fontWeight;
    	    	if(weight != "none"){
    	    		weight = "none";
    	    	}else{
    	    		weight = "bolder";
    	    	}
    	    	selectele.style.fontWeight = weight;
    	    };

    	    //修改文字下划线
    	    $scope.toggleTextDecor = function() {
    	    	var selectele = $scope.selectEle.ele;
    	    	var decor = selectele.style.textDecoration;
    	    	if(decor != "none"){
    	    		decor = "none";
    	    	}else{
    	    		decor = "underline";
    	    	}
    	    	selectele.style.textDecoration = decor;
    	    };

    	    //修改文字斜体
    	    $scope.toggleFontStyle = function() {
    	    	var selectele = $scope.selectEle.ele;
    	    	var style = selectele.style.fontStyle;
    	    	if(style != "none"){
    	    		style = "none";
    	    	}else{
    	    		style = "italic";
    	    	}
    	    	selectele.style.fontStyle = style;
    	    };
    	    
    	    //修改文字斜体
    	    $scope.setTextAlign = function($event) {
    	    	var selectele = $scope.selectEle.ele;
    	        var textAlign = $event.target.getAttribute("align");
    	        selectele.style.textAlign = textAlign;
    	    };
    	    

    	    $scope.colors = [{
    	        color:"red"
    	    },{
    	        color:"green"
    	    },{
    	        color:"#980000"
    	    },{
    	        color:"#ff0000"
    	    },{
    	        color:"#ff9900" 
    	    },{
    	        color:"#ffff00"    
    	    },{
    	        color:"#00ff00"
    	    },{
    	        color:"rgba(0,255,209,100)"
    	    },{
    	        color:"#00ffff"
    	    },{
    	        color:"#9900ff"
    	    },{
    	        color:"#ff00ff"
    	    },{
    	        color:"#FFFFFF"
    	    },{
    	    	color:"#0000FF"
    	    },{
    	        color:"rgba(0,0,0,0)",
    	        content:"无"
    	    }];

    	    //文字颜色
    	    $scope.chooseTextColor = function(color) {
    	    	var selectele = $scope.selectEle.ele;
    	        selectele.style.color = color;
    	        $scope.showTextColorPicker = false;
    	    };
    	    
    	    $scope.showTextColorPanel = function() {
    	        $scope.showTextColorPicker = true;
    	    };
    	    
    	    //边框颜色
    	    $scope.chooseBorderColor = function(color) {
    	    	var selectele = $scope.selectEle.ele;
    	        selectele.style.borderColor = color;
    	        $scope.showBorderColorPicker = false;
    	    };
    	    
    	    $scope.showBorderColorPanel = function() {
    	        $scope.showBorderColorPicker = true;
    	    };
    	    
    	    //元素背景颜色
    	    $scope.chooseBackEleColor = function(color) {
    	    	var selectele = $scope.selectEle.ele;
    	        selectele.style.background = color;
    	        $scope.showBackEleColorPicker = false;
    	    };
    	    
    	    $scope.showBackEleColorPanel = function() {
    	        $scope.showBackEleColorPicker = true;
    	    };
    	    
    	    //背景颜色修改
    	    $scope.chooseBackColor = function(color) {
    	        $scope.page.backstyle.background = color;
    	        $scope.showBackColorPicker = false;
    	    };
    	    
    	    $scope.showBackColorPanel = function() {
    	        $scope.showBackColorPicker = true;
    	    };
    	    
    	    $scope.editAttr ="text";
    	    
    	    //编辑动作或文本的切换
    	    $scope.editType = function(type) {
    	    	$scope.editAttr = type;
    	    };
    	    
    	    $scope.animateActions = [{
    	        actionName : "弹性",
    	        actionClass :"animated bounceIn",
    	        img:""
    	    },{
    	        actionName : "从左弹性飞入",
    	        actionClass :"animated bounceInLeft",
    	        img:""
    	    },{
    	        actionName : "从右弹性飞入",
    	        actionClass : "animated bounceInRight",
    	        img:""
    	    },{
    	        actionName : "从上弹性飞入",
    	        actionClass :"animated bounceInUp",
    	        img:""
    	    },{
    	        actionName : "从下弹性飞入",
    	        actionClass : "animated bounceInDown",
    	        img:""
    	    },{
    	        actionName : "渐显",
    	        actionClass : "animated fadeIn",
    	        img:""
    	    },{
    	        actionName : "从左渐显飞入",
    	        actionClass : "animated fadeInLeft",
    	        img:""
    	    },{
    	        actionName : "从右渐显飞入",
    	        actionClass :"animated fadeInRight",
    	        img:""
    	    },{
    	        actionName : "从上渐显飞入",
    	        actionClass :"animated fadeInUp",
    	        img:""
    	    },{
    	        actionName : "从下渐显飞入",
    	        actionClass : "animated fadeInDown",
    	        img:""
    	    },{
    	        actionName : "从左滑入",
    	        actionClass :"animated slideInLeft",
    	        img:""
    	    },{
    	        actionName : "从右滑入",
    	        actionClass : "animated slideInRight",
    	        img:""
    	    },{
    	        actionName : "从上滑入",
    	        actionClass :"animated slideInUp",
    	        img:""
    	    },{
    	        actionName : "从下滑入",
    	        actionClass : "animated slideInDown",
    	        img:""
    	    },{
    	        actionName : "闪现",
    	        actionClass : "animated zoomIn",
    	        img:""
    	    },{
    	        actionName : "从左闪现",
    	        actionClass : "animated zoomInLeft",
    	        img:""
    	    },{
    	        actionName : "从右闪现",
    	        actionClass : "animated zoomInRight",
    	        img:""
    	    },{
    	        actionName : "从上闪现",
    	        actionClass : "animated zoomInUp",
    	        img:""
    	    },{
    	        actionName : "从下闪现",
    	        actionClass : "animated zoomInDown",
    	        img:""
    	    },{
    	        actionName : "从左滚入",
    	        actionClass : "animated rotateInDownLeft",
    	        img:""
    	    },{
    	        actionName : "从右滚入",
    	        actionClass : "animated rotateInDownRight",
    	        img:""
    	    },{
    	        actionName : "摇摆",
    	        actionClass : "animated shake",
    	        img:""
    	    },{
    	        actionName : "闪烁",
    	        actionClass : "flash",
    	        img:""
    	    },{
    	        actionName : "脉动",
    	        actionClass : "animated pulse",
    	        img:""
    	    },{
    	        actionName : "tada",
    	        actionClass : "animated tada",
    	        img:""
    	    },{
    	        actionName : "颤动",
    	        actionClass : "animated wobble",
    	        img:""
    	    },{
    	        actionName : "快速翻转",
    	        actionClass : "animated flip",
    	        img:""
    	    },{
    	        actionName : "上下翻转",
    	        actionClass : "animated flipInX",
    	        img:""
    	    },{
    	        actionName : "左右翻转",
    	        actionClass : "animated flipInY",
    	        img:""
    	    },{
    	        actionName : "刹车",
    	        actionClass : "animated lightSpeedIn",
    	        img:""
    	    },{
    	        actionName : "旋转",
    	        actionClass : "animated rotateIn",
    	        img:""
    	    },{
    	        actionName : "旋入",
    	        actionClass : "animated rollIn",
    	        img:""
    	    }];
    	    
    	    //修改选中元素动画
    	    $scope.chooseAction = function(action) {
    	    	var selectele = $scope.selectEle.ele;
    	        $("div[id='"+$scope.selectEle.id+"']").css({//让动画立即显示
    	             webkitAnimationDuration:"1s",
    	             webkitAnimationDelay:"0s",
    	        });
    	        selectele.entrance = action.actionClass;
    	    };

    	    //添加文本元素 
    	    $scope.addText = function() {
    	    	var id = UUID.getUUID();
    	        $scope.page.eles.push({//Text元素
    	          id:id,
    	      	  type:"text",
    	          style:{ 
    	                  position:"absolute",
    	                  left:20,
    	                  top:20,
    	                  width:100,
    	                  height:100,
    	                  wordWrap:"break-word",
    	                  color:"red",//文字颜色
    	                  fontSize:20,//文字大小
    	                  fontWeight:"bolder",//文字加强
    	                  fontStyle:"italic",//文字风格
    	                  textDecoration:"underline",//下划线
    	                  background:"yellowgreen",//背景色
    	                  lineHeight:2,//行距
    	                  borderWidth:2,//边框宽度
    	                  borderStyle:"solid",//边框样式
    	                  borderColor:"yellow",//边框颜色
    	                  borderRadius:25,//圆角
    	                  webkitTransform:"rotate(0deg)",//旋转
    	                  rotate:0,//旋转
    	                  opacity:1,
    	                  boxShadowWidth:100,//阴影
    	                  boxShadow:"0 0 100px black",//阴影
    	                  textAlign:"center",
    	                  webkitAnimationDuration:"1s",//动画持续时间
    	                  animateTime:1,
    	                  webkitAnimationDelay:"1s",//动画延时
    	                  animateDelay:1,
    	                  zIndex:3
    	          },
    	          content:"",
    	          entrance:"animated zoomIn"
    	        });
    	    };
    	    
    	    //添加图片
    	    $scope.addImg = function() {
    	    	var id = UUID.getUUID();
    	    	var src = $scope.selectBackImgSrc;
    	    	if(src) {
    	    		$scope.page.eles.push({//Text元素
    	    	          id:id,
    	    	      	  type:"img",
    	    	      	  src:$scope.selectBackImgSrc,
    	    	            style:{
    	    	                position:"absolute",
    	    	                left:120,
    	    	                top:120,
    	    	                width:100,
    	    	                height:100,
    	    	                borderRadius:25,//圆角
    	    	                webkitTransform:"rotate(0deg)",//旋转
    	    	                rotate:0,//旋转
    	    	                opacity:1,
    	    	                boxShadowWidth:100,//阴影
    	    	                boxShadow:"0 0 100px black",//阴影
    	    	                textAlign:"center",
    	    	                webkitAnimationDuration:"1s",//动画持续时间
    	    	                animateTime:1,
    	    	                webkitAnimationDelay:"1s",//动画延时
    	    	                animateDelay:1,
    	    	                zIndex:3
    	    	        },
    	    	        entrance:"animated zoomIn"
    	    	      });
    	    		$scope.showPictureStroe = false;//关闭图片显示库
    	        	$scope.selectBackImgSrc = "";//去除图片库中图片选中样式
    	    		$("div[edit]").one("webkitAnimationEnd",function(){//去掉动画的样式才可以修改透明度和旋转等样式
    		                $(this).removeClass();
    		        });
    	    	}
    	        
    	    };
    	    
    	    var fontSizes = [];//字体大小
    	    for(var i=12;i<50;i++) {
    	       fontSizes.push({
    	          id:i,
    	          size:i
    	       });
    	    }
    	    $scope.fontSizes = fontSizes;
    	   
    	   $scope.changePageEntranceAnimation = function() { //改变页面进入动画
    	       var className = $scope.page.entrance;
    	       $("#pageWrap div:first").addClass(className).one('webkitAnimationEnd',function() {
    	             $(this).removeClass();
    	       });
    	   };
    	   
    	   $scope.changePageExitAnimation = function() { //改变页面离开动画
    	       var className = $scope.page.exit;
    	       $("#pageWrap div:first").addClass(className).one('webkitAnimationEnd',function() {
    	             $(this).removeClass();
    	       });
    	   };

    	   $scope.upPage = function(page,$event) {
    	        var index = page.index;
    	        var prevpage = getPrevPage(index);
    	        prevpage.index = prevpage.index+1;
    	        page.index = page.index-1;

    	        $event.stopPropagation();
    	   };

    	   $scope.downPage = function(page,$event) {

    	       var index = page.index;
    	       var nextpage = getNextPage(index);
    	       nextpage.index = nextpage.index-1;
    	       page.index = page.index+1;

    	       $event.stopPropagation();
    	   };

    	    var getPage = function(pageId) { //根据pageId返回对应页面对像
    	        var pages = $scope.pages;
    	        var length = pages.length;
    	        var page = "";
    	        for(var i=0;i<length;i++) {
    	            if(pages[i].pageId == pageId) {
    	                page = pages[i];
    	                break;
    	            }
    	        }
    	        return page;
    	    };

    	   var getPrevPage = function(index) { //根据index返回对应页面的前一页页面对像
    	        if(index > 1) {
    	            var pages = $scope.pages;
    	            var length = pages.length;
    	            var page = "";
    	            for(var i=0;i<length;i++) {
    	                if(pages[i].index == (index-1)) {
    	                    page=pages[i];
    	                    break;
    	                }
    	            }
    	            return page;
    	        }        
    	    };
    	    
    	    var getNextPage = function(index) { //根据index返回对应页面的后一个页页面对像
    	        var pages = $scope.pages;
    	        var length = pages.length;
    	        if(index < length) {       
    	            for(var i=0;i<length;i++) {
    	                if(pages[i].index == (index+1)) {
    	                    page=pages[i];
    	                    break;
    	                }
    	            }
    	            return pages[i];
    	        }        
    	    };

    	    $scope.addPage = function() {//添加页面
    	         var length = $scope.pages.length;
    	         var page_uuid = UUID.getUUID();
    	         $scope.pages.push({
    	                index:length+1,
    	                pageId:page_uuid,
    	                entrance:"animated zoomIn",
    	                exit:"animated zoomOut",
    		            backImg:{
    		              src:"lib/images/bg3.jpg",
    	                  style:{
    	                     position:"absolute",
    	                     width:300,
    	                     height:470,
    	                     left:0,
    	                     top:0,    
    	                     opacity:1                    
    	                  }
    		            },
    		            backstyle:{
    		              background:"#FFF"  
    		            },
    		            eles:[]
    	              });
    	         return page_uuid;
    	    };
    	    
    	    //上传图片
    	    $scope.upload = function (files) {
    	        if (files && files.length) {
    	            for (var i = 0; i < files.length; i++) {
    	                var file = files[i];
    	                Upload.upload({
    	                    url: 'uploadbackimg.do',
    	                    file: file
    	                }).progress(function (evt) {
    	                    var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
    	                    console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
    	                }).success(function (data, status, headers, config) {
    	                	var id = UUID.getUUID();
    	                	$scope.backImgs.push({
    	                    	id:id,
    	                    	groupName:"my",
    	                    	typeName:"",
    	                    	fileName:data.data.fileName,
    	                    	img:"lib/images/UserbackImg/" + data.data.filesrc,
    	                    	width:data.data.width,
    	                    	height:data.data.height,
    	                    	ispub:false,
    	                    	userid:"",
    	                    	description:"图片",
    	                    	status:1
    	                    });
    	                });
    	            }
    	        }
    	    };
    	    
    	    //选中背景图片
    	    $scope.selectBackImg = function(src) {
    	    	if($scope.selectBackImgSrc == src) {//重复点选就取消选择
    	    		$scope.selectBackImgSrc = "";
    	    	}else{
    	    		$scope.selectBackImgSrc = src;//添加图片库中图片选中样式
    	    	}
    	    };
    	    
    	    //JCrop截图时事件,用于保存背景图片的样式大小位置
    	    var setChange = function (c){
    	        var x = c.x;
    	        var y = c.y;
    	        var x2 = c.x2;
    	        var y2 = c.y2;
    	        var width = c.w;
    	        var height = c.h;

    	        var times = 150/width;
    	        var timesx = x/150;
    	        var timesy = y/235;
    	        var style = $scope.page.backImg.style;
    	        style.width = 300*times;
    	        style.height = 470*times;
    	        style.left = -style.width*timesx;
    	        style.top = -style.height*timesy;
    	        $scope.$apply();
    	  }
    	    
    	    
    	    //设置背景图片
    	    $scope.setBackImg = function() {
    	    	var src = $scope.selectBackImgSrc;
    	    	var backImg = $scope.page.backImg;
    	    	var style = backImg.style;
    	    	backImg.src = src;
    	    	style.width = 300;
    	    	style.height = 470;
    	    	style.left = 0;
    	    	style.top = 0;
    	    	if($scope.jcrop) {
    	    		$scope.jcrop.destroy();//更换背景图片先暂停jquery剪裁器  
    	    	}
    	    	$(".backimgWrap img").Jcrop({
    		        onChange:setChange,
    		        onSelect:setChange,
    		        aspectRatio:0.638
    		    },function(){
    		        $scope.jcrop = this;
    		    });
    	        $scope.jcrop.setImage(src);//设置需要改变的图片地址
    	    	$scope.showPictureStroe = false;//关闭图片显示库
    	    	$scope.selectBackImgSrc = "";//去除图片库中图片选中样式
    	    };
    	    
    	    //更换图片
    	    $scope.changeImg = function() {
    	    	var selectele = $scope.selectEle.ele; 
    	    	var src = $scope.selectBackImgSrc;
    	    	selectele.src = src;
    	    	$scope.showPictureStroe = false;//关闭图片显示库
    	    	$scope.selectBackImgSrc = "";//去除图片库中图片选中样式
    	    };
    	    
    	    //移除背景图片
    	    $scope.removeBackImg = function() {
    	    	$scope.page.backImg.src = "";
    	    	$scope.jcrop.destroy();//移出背景图片先暂停jquery剪裁器  
    	    };
    	    
    	    //显示图片选择库
    	    $scope.showPictureStore = function(use) {
    	    	$scope.showPictureStroe = true;//显示
    	    	$scope.showPictureStroeFor = use;
    	    };
    	    
    	    
    	    $scope.backImgs = [{
    	    	id:"1234",
    	    	groupName:"背景",
    	    	typeName:"风景",
    	    	fileName:"bg1.jpg",
    	    	img:"lib/images/UserbackImg/bg1.jpg",
    	    	width:1024,
    	    	height:707,
    	    	ispub:true,
    	    	userid:"",
    	    	description:"图片",
    	    	status:1
    	    },{
    	    	id:"12345",
    	    	groupName:"背景",
    	    	typeName:"风景",
    	    	fileName:"bg2.jpg",
    	    	img:"lib/images/UserbackImg/bg2.jpg",
    	    	width:360,
    	    	height:220,
    	    	ispub:true,
    	    	userid:"",
    	    	description:"图片",
    	    	status:1
    	    },{
    	    	id:"12346",
    	    	groupName:"背景",
    	    	typeName:"风景",
    	    	fileName:"bg3.jpg",
    	    	img:"lib/images/UserbackImg/bg3.jpg",
    	    	width:604,
    	    	height:1008,
    	    	ispub:true,
    	    	userid:"",
    	    	description:"图片",
    	    	status:1
    	    }];
    	    
    	    //是否使用网格
    	    $scope.useGridOrNot = function() {
    	    	if($scope.useGrid) {
    	    		$scope.useGrid = false;
    	    	}else {
    	    		$scope.useGrid = true;
    	    	}
    	    }
    	    
    	    //复制页面
    	    $scope.copyPage = function() {
    	    	var page = $scope.page;
    	    	var index = page.index;
    	    	var str = JSON.stringify(page);
    	    	
    	    	var newPage = {};
            	var str = JSON.stringify(page);
            	var copy = eval("("+str+")");
            	for(key in copy) {
            		if(key != "$$hashKey") {
            			newPage[key] = copy[key];
            		} 
            	}
                newPage.pageId = UUID.getUUID();
                newPage.index = index + 1;
                $scope.pages.splice(index,0,newPage);
                var length = $scope.pages.length;
                for(var i=0;i<length;i++) {
                	$scope.pages[i].index = i+1;
                }
    	    }
    	    
    	    //删除页面
    	    $scope.delePage = function() {
    	    	var index = $scope.page.index;
    	    	var pages = $scope.pages;
    	    	var length = pages.length;
    	    	pages.splice(index-1,1);
    	    	length = pages.length;
    	    	for(var i=0;i<length;i++) {
    	    		pages[i].index = i+1;
    	    	}
    	    	if(index > 1) {
    	    		$scope.choosePage(pages[index-2].pageId);
    	    	}else {
    	    		if(length == 0 ) {
    	    			var pageId = $scope.addPage();
    	    			$scope.choosePage(pageId);
    	    		}else{
    	    			$scope.choosePage(pages[0].pageId);
    	    		}
    	    	}
    	    }
    	    
    	    //预览项目
    	    $scope.reviewProject = function() {
    	    	var projectId = $("div[projectId]").attr("projectId");
    	    	$window.location.href = "showPage.do?projectId=" + projectId;
    	    };
    	    
    	    //取消loading效果
    	    $scope.loading = false;
    	    
    });
});

 